package org.orbroker.config

import org.orbroker.Token
import org.orbroker.exception._

/**
 * Utility class for token id verification.
 * @see BrokerBuilder#verify(Set[Symbol])
 * @author Nils Kilden-Pedersen
 *
 */
abstract class TokenSet(nameMustMatchID: Boolean) {
  /**
   * Set of ids defined in this class
   */
  lazy val idSet: Set[Symbol] = {
    var set: Set[Symbol] = Set.empty
    for (method ← getClass.getMethods if method.getReturnType eq classOf[Token[_]]) {
      if (method.getParameterTypes.length == 0) {
        val token = method.invoke(TokenSet.this).asInstanceOf[Token[_]]
        if (nameMustMatchID && method.getName != token.id.name) {
          val msg = getClass.getName + "." + method.getName + " does not match id: " + token.id.name
          throw new ConfigurationException(msg)
        }
        set += token.id
      }
    }
    set
  }
}